<template>
	<view class="container">
		<!-- 顶部导航栏 -->
<!-- 		<view class="nav-bar">
			<view class="left">
				<u-icon name="arrow-left" size="32" color="#333" @click="goBack"></u-icon>
			</view>
			<view class="title">{{questionaire.name}}</view>
			<view class="right"></view>
		</view> -->
		
		<!-- 内容区域 -->
		<view class="content">
			<view class="question-list">
				<view class="question-item" v-for="(question, qIndex) in questionaire.questionaireQuestionList" :key="question.id">
					<view class="question-title">{{qIndex}}. {{question.content}}</view>
					<view class="options">
						<view class="option" v-for="(option, oIndex) in question.questionaireSelectVoList" :key="option.id"
							:class="{'selected': userAnswers[question.id] === option.id}"
							@click="selectOption(question.id, option.id)">
							<text class="option-text">{{option.content}}</text>
						</view>
					</view>
				</view>
			</view>
			
			<view class="submit-btn" @click="submitForm">
				<text>提交评价</text>
			</view>
		</view>
	</view>
</template>

<script>
	import { getQuestionaireAllDetail, submitAnswers } from '@/common/api';
	export default {
		data() {
			return {
				questionaireId:'',
				questionaire:[],
				userAnswers:{}
			}
		},
		onLoad(options) {
			this.questionaireId = options.id;
			this.loadData();
		},
		computed: {
		},
		methods: {
			async loadData(){
				try {
					const qRes = await getQuestionaireAllDetail(this.questionaireId);
					this.questionaire = qRes.data;
					console.log(this.questionaire);
				} catch (e) {
					console.error('加载问卷失败', e);
				}
			},
			goBack() {
				uni.navigateBack()
			},
			selectOption(questionId, optionId) {
				this.userAnswers[questionId] = optionId;
			},
			async submitForm() {
				const allQuestions = this.questionaire.questionaireQuestionList || [];
				
				// 1. 校验是否每题都有答案
				const unanswered = allQuestions.filter(q => !(q.id in this.userAnswers));
				if (unanswered.length > 0) {
					uni.showToast({
						title: '请完成所有题目再提交',
						icon: 'none'
					});
					return;
				}
				
				// 2. 构造提交数据
				const data = Object.entries(this.userAnswers).map(([questionId, selectId]) => ({
					questionId,
					selectId
				}));
				
				// 3. 发起提交请求
				try {
					const res = await submitAnswers(data);
					uni.showToast({
						title: '感谢您的反馈',
						icon: 'success'
					});
					// 提交成功后延时返回上一个页面
					setTimeout(() => {
						uni.navigateBack();
					}, 1500);
				} catch (error) {
					uni.showToast({
						title: error.message || '网络错误',
						icon: 'none'
					});
				}
			}
		}
	}
</script>

<style lang="scss">
	.container {
		min-height: 100vh;
		background-color: #F7F7FA;
		
		.nav-bar {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 20rpx 30rpx;
			background-color: #fff;
			
			.title {
				font-size: 36rpx;
				font-weight: bold;
				color: #333;
			}
		}
		
		.content {
			padding: 30rpx;
			padding-bottom: 120rpx;
			
			.question-list {
				.question-item {
					background-color: #fff;
					border-radius: 16rpx;
					padding: 30rpx;
					margin-bottom: 30rpx;
					
					.question-title {
						font-size: 32rpx;
						color: #333;
						margin-bottom: 20rpx;
						line-height: 1.5;
					}
					
					.options {
						display: flex;
						flex-direction: column;
						gap: 20rpx;
						
						.option {
							display: flex;
							align-items: center;
							padding: 20rpx;
							border-radius: 8rpx;
							background-color: #F7F7FA;
							
							.option-text {
								font-size: 32rpx;
								color: #666;
							}
							
							&.selected {
								background-color: #E6F7FF;
								
								.option-text {
									color: #1890FF;
								}
							}
						}
					}
				}
			}
			
			.suggestion {
				background-color: #fff;
				border-radius: 16rpx;
				padding: 30rpx;
				margin-bottom: 30rpx;
				
				.title {
					font-size: 30rpx;
					color: #333;
					margin-bottom: 20rpx;
				}
				
				textarea {
					width: 100%;
					height: 200rpx;
					font-size: 30rpx;
					color: #333;
					line-height: 1.5;
					padding: 20rpx;
					background-color: #F7F7FA;
					border-radius: 8rpx;
				}
				
				.word-count {
					text-align: right;
					font-size: 26rpx;
					color: #999;
					margin-top: 10rpx;
				}
			}
			
			.submit-btn {
				position: fixed;
				bottom: 40rpx;
				left: 30rpx;
				right: 30rpx;
				height: 88rpx;
				background-color: #1890FF;
				border-radius: 44rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				
				text {
					font-size: 32rpx;
					color: #fff;
					font-weight: bold;
				}
				
				&.disabled {
					background-color: #ccc;
					opacity: 0.6;
				}
			}
		}
	}
</style> 